using System;
using System.Diagnostics;
using System.Windows;

namespace ChessKit.Common
{
  public abstract class Colorizer<T> : IColorizer
  {
    public DependencyProperty Property { get; private set; }

    protected Colorizer(DependencyProperty property)
    {
      Debug.Assert(property.PropertyType.IsAssignableFrom(typeof(T)));
      Property = property;
    }

        event EventHandler IColorizer.Update
    {
      add { _update += value; }
      remove { _update -= value; }
    }

    private EventHandler _update;

    private void OnUpdate(EventArgs e)
    {
      var handler = _update;
      if (handler != null) handler(this, e);
    }

    public void Refresh()
    {
      OnUpdate(EventArgs.Empty);
    }

    object IColorizer.Test(BoardLocation location)
    {
      return TestCore(location);
    }

    protected abstract T TestCore(BoardLocation location);
  }
}